var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: getApp().globalData.url,
    username: "",
    //显示数据
    patient: [],
    scrollHeight: 0,
    currentId: -1,
    currentDate: util.formatDate(new Date()),
    chooseDate: util.formatDate(new Date()),
    chooseFlag: 'false',
    patient1: [{
      logoLink: '/images/defaultImage.png',
      patientName: '小红',
      orderDate: '2020-11-31',
      orderTime: '上午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小明',
      orderDate: '2020-12-11',
      orderTime: '下午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小白',
      orderDate: '2020-11-31',
      orderTime: '上午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小刘',
      orderDate: '2020-12-09',
      orderTime: '上午',
      patientTel: "18727508188"
    },
    {
      logoLink: '/images/defaultImage.png',
      patientName: '小兰',
      orderDate: '2020-12-05',
      orderTime: '下午',
      patientTel: "18727508188"
    },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();
    this.setData({
      username: getApp().globalData.username,
      chooseFlag: 'false'
    })
    this.getPatient();
    this.enterPage(5);
    
    var that = this;
    //动态scrollview
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scrollHeight: (windowHeight - 0 - 60) * 750 / windowWidth
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      username: getApp().globalData.username,
      chooseFlag: 'false'
    })
    this.getPatient();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.enterPage(5);
    console.log(5)
    this.setData({
      username: getApp().globalData.username,
      chooseFlag: 'false'
    })
    this.getPatient();

    try {
      var ap = wx.getStorageSync('patient')
      this.setData({
        patient: ap
      })
    } catch(e) { }
  },

  /**
   * 进入appointments页面，通知后端将进入此页面次数加一
   */
  enterPage: function (e) {
    var that = this
    wx.request({
      url: this.data.url + 'patientsettablebar',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        unitphone: getApp().globalData.username,
        pageNum: e
      },
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      chooseFlag: 'false'
    })
    this.getPatient()
  },

  /**
   * 按日期查找预约患者列表
   */
  bindDateChange: function(e) {
    this.setData({
      chooseDate: e.detail.value
    });
    this.data.chooseFlag = 'true';
    this.getPatient()
  },

  /**
   * 获取病人信息
   */
  getPatient: function () {
    var that = this
    var tel = this.data.username
    var flag = this.data.chooseFlag
    var date = this.data.chooseDate
    wx.request({
      url: this.data.url + 'getPatientServlet',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        chooseFlag: flag,
        chooseDate: date,
        doctorTel: tel
      },
      success: function (res) {
        if (res.data != false && res.data != "false" && res.data != "空") {
          try {
            wx.setStorageSync("patient", res.data)
          } catch (e) { }
          that.setData({
            patient: res.data
          })
        }
        else if(res.data == "空") {
          that.setData({
            patient: null
          })
        }
        else {
          wx.showToast({
            title: '获取失败，请从顶部下拉刷新',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: '获取失败，请从顶部下拉刷新',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 滑动事件
   */
  backToStart: function(e) {
    if (this.data.currentId != e.currentTarget.dataset.idx && this.data.currentId != -1) {
      this.setData({
        scl: 0
      })
    }
    this.setData({
      currentId: e.currentTarget.dataset.idx
    })
  },

  /**
   * 发送消息
   */
  sendMessage: function(e) {
    var id = e.currentTarget.dataset.idx
    var tel = this.data.patient[id].patientTel
    var orderDate = this.data.patient[id].orderDate
    var orderTime = this.data.patient[id].orderTime
    
    wx.request({
      url: this.data.url + 'sendMessageServlet',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorTel: getApp().globalData.username,
        patientTel: tel,
        bookDate: orderDate,
        bookTime: orderTime,
      },
      success(res) {
        wx.showToast({
          title: '已成功发送消息提醒该患者就诊',
          icon: 'success',
        })
      },
      fail(res) {
        wx.showToast({
          title: '信息发送失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 跳转病人信息
   */
  clickPatient: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var patientTel = JSON.stringify(that.data.patient[index].patientTel)
    var patientOrderDate = JSON.stringify(that.data.patient[index].orderDate)
    var patientName = JSON.stringify(that.data.patient[index].patientName)
    wx.navigateTo({
      url: '../patientMessage/patientMessage?patientTel=' + patientTel + '&&patientOrderDate=' + patientOrderDate + '&&name=' + patientName,
    })
  },

  /**
   * 医生点击取消该患者的预约
   */
  cancelAppointment: function(e) {
    var id = e.currentTarget.dataset.idx
    var tel = this.data.patient[id].patientTel
    var orderDate = this.data.patient[id].orderDate
    var orderTime = this.data.patient[id].orderTime
    var that = this
    wx.showModal({
      title: '确定取消预约？',
      success (res) {
        if (res.confirm) {
          wx.request({
            url: that.data.url + 'doctorCancelAppointment',
            method: 'POST',
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              doctorTel: getApp().globalData.username,
              patientTel: tel,
              bookDate: orderDate,
              bookTime: orderTime,
            },
            success(res) {
              wx.showToast({
                title: '已成功取消此预约',
                icon: 'success',
              })
              that.getPatient()
            },
            fail(res) {
              wx.showToast({
                title: '预约取消失败',
                icon: 'none',
                duration: 2000
              })
            }
          })
        } 
        else if (res.cancel) {
          wx.showToast({
            title: '未取消该预约',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },

  /**
   * 跳转到今日预约
   */
  navigateToTodayAppointment: function(){
    wx.navigateTo({
      url: '../todayAppointments/todayAppointments',
    })
  },

  /**
   * 医生点击与患者交谈
   */
  chatWithPatient: function(e) {
    var that = this
    //拿到点击的index下标
    var index = e.currentTarget.dataset.idx
    //将对象转为string
    var patientTel = JSON.stringify(this.data.patient[index].patientTel)
    var patientName = JSON.stringify(this.data.patient[index].patientName)
    wx.navigateTo({
      url: '../onlineConsulting/onlineConsulting?otherTel=' + patientTel + '&&otherName=' + patientName,
    })
  }
})
